package com.taocore.top.title.action;

import java.sql.Connection;
import java.util.List;
import java.util.logging.Level;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.taocore.top.title.db.DBUtils;
import com.taocore.top.title.db.Dao;
import com.taocore.top.title.domain.TitleHistoryItem;

@Results({
	  @Result(location="title-history.jsp")
})
public class TitleHistoryAction extends ActionBase{

	public static final int MAX_ITEMS = 10;
	
	private long numIid;
	private List<TitleHistoryItem> history;
	
	public String execute()
	{
		Connection conn = null;
		try
		{
			conn = DBUtils.getConnection(); 
			history = Dao.INSTANCE.getTitleHistory(numIid, conn);
			int size = history.size();
			if (size > MAX_ITEMS)
			{
				List<TitleHistoryItem> oldItems = history.subList(MAX_ITEMS, size);
				Dao.INSTANCE.deleteTitleHistory(oldItems, conn);
			}
		}
		catch (Exception e) {
			_log.log(Level.SEVERE, "", e);
		}
		finally
		{
			DBUtils.close(conn, null, null);
		}
		return SUCCESS;
	}

	public void setNumIid(long numIid) {
		this.numIid = numIid;
	}

	public long getNumIid() {
		return numIid;
	}
	
	public void setHistory(List<TitleHistoryItem> history) {
		this.history = history;
	}

	public List<TitleHistoryItem> getHistory() {
		return history;
	}
}
